import ar.com.coonocer.joy.mdg.templatehelpers.AttributeUtil;
import ar.com.coonocer.joy.mdg.templatehelpers.MultiplicityUtil;
import ar.com.coonocer.joy.mdg.templatehelpers.NamingUtil;
import ar.com.coonocer.joy.mdg.xmi.core.Association;
import ar.com.coonocer.joy.mdg.xmi.core.AssociationEnd;
import ar.com.coonocer.joy.mdg.xmi.core.Attribute;
import ar.com.coonocer.joy.mdg.xmi.core.Class
import ar.com.coonocer.joy.mdg.xmi.model.Package

public class XMIAssociationHelper{

	public static String getXMI(Association assoc){
		StringBuilder sb = new StringBuilder();
		//
		String name=generateAssocName(assoc);
		//
		sb.append("								<UML:Association name=\"${name}\" xmi.id=\"${XMIGenerator.util.nextXmiId}\" visibility=\"public\" isRoot=\"false\" isLeaf=\"false\" isAbstract=\"false\">\r\n");
		sb.append("									<UML:Association.connection>\r\n");
		//
		sb.append(getXMIForAssocitationEnd(assoc.connection[0]));
		sb.append(getXMIForAssocitationEnd(assoc.connection[1]));
		//
		sb.append("									</UML:Association.connection>\r\n");
		sb.append("								</UML:Association>\r\n");
		//
		return sb.toString();
	}

	public static String getXMIForAssocitationEnd(AssociationEnd assocEnd){
		StringBuilder sb = new StringBuilder();
		//
		String typeXmiId=XMIClassHelper.getXmiIdForClass(assocEnd.participant);
		Integer lower=MultiplicityUtil.getLower(assocEnd.multiplicity);
		String upper=MultiplicityUtil.getUpper(assocEnd.multiplicity);
		String isNavigable=upper.equals("1");
		String name =""; //assocEnd.name
		//
		sb.append("										<UML:AssociationEnd visibility=\"public\" name=\"${name}\" aggregation=\"none\" isOrdered=\"false\" targetScope=\"instance\" changeable=\"none\" isNavigable=\"${isNavigable}\" type=\"${typeXmiId}\">\r\n");
		sb.append("											<UML:AssociationEnd.multiplicity>\r\n");
		sb.append("												<UML:Multiplicity>\r\n");
		sb.append("													<UML:Multiplicity.range>\r\n");
		sb.append("														<UML:MultiplicityRange lower=\"${lower}\" upper=\"${upper}\"/>\r\n");
		sb.append("													</UML:Multiplicity.range>\r\n");
		sb.append("												</UML:Multiplicity>\r\n");
		sb.append("											</UML:AssociationEnd.multiplicity>\r\n");
		sb.append("											<UML:AssociationEnd.participant>\r\n");
		sb.append("												<UML:Class xmi.idref=\"${typeXmiId}\"/>\r\n");
		sb.append("											</UML:AssociationEnd.participant>\r\n");
		sb.append("										</UML:AssociationEnd>\r\n");
		//
		return sb.toString();
	}

	private static String generateAssocName(Association assoc){
		String name="";
		if (assoc.name != null) {
			name = assoc.name;
			if (name.startsWith("(")) {
				name = name.substring(1);
			}
			if (name.endsWith(")")) {
				name = name.substring(0, name.length()-1);
			}
			int pos = name.indexOf("=");
			if (pos != -1) {
				String attName1 = name.substring(0, pos);
				String attName2 = name.substring(pos+1);
				//
				attName1 = NamingUtil.toLowerCamelCase(attName1.trim());
				attName2 = NamingUtil.toLowerCamelCase(attName2.trim());
				//
				name="("+attName1+" = "+attName2+")";
			}
		}
		return name;
	}
}